package com.imooc.api.controller.user;

import com.imooc.grance.result.GraceJsonResult;
import com.imooc.pojo.vo.AppUserInfoVO;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.RedisOperator;
import com.mongodb.client.gridfs.GridFSBucket;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 *
 * @auther Levi
 * @Date: 2021/03/13/20:34
 * @Description:
 */
@Component
public class BaseControllerApi {


    @Autowired
    public RedisOperator redis;
    @Autowired
    public RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    public static final String MOBILE_SMSCODE = "mobile:smscode";
    public static final String REDIS_USER_TOKEN = "redis_user_token";
    public static final Integer COOKIE_MONTH = 30 * 24 * 60 * 60;
    public static final Integer COOKIE_DELETE = 0;
    public static final String REDIS_USER_INFO = "redis_user_info";
    public static final String REDIS_ADMIN_TOKEN = "redis_admin_token";
    public static final String ADMIN_TOKEN_KEY = "atoken";
    public static final String ADMIN_ID_KEY = "aid";
    public static final String ADMIN_NAME_KEY = "aname";
    public static final String REDIS_ALL_CATEGORY = "redis_all_category";
    public static final Integer COMMON_PAGE = 1;
    public static final Integer COMMON_PAGE_SIZE = 10;
    public static final String REDIS_ARTICLE_READ_COUNTS = "redis_article_read_counts";
    public static final String REDIS_ALREADY_READ = "redis_already_read";
    public static final String FILE_PATH = "E:/IdeaProjects/imooc-news-dev/imooc-news-dev-service-files/src/face" +
            "images";


    @Value("${website.domain-name}")
    public String DOMAIN_NAME;

//    /**
//     * 校验有没有错误的方法
//     * @param result
//     * @return
//     */
//    public Map<String, String> getErrors(BindingResult result) {
//        Map<String, String> map = new HashMap<>();
////        List<ObjectError> errorList = result.getAllErrors();
//        //获取所有的错误信息的列表
//        List<FieldError> errorList = result.getFieldErrors();
//        for (FieldError error : errorList) {
//            String fieldName = error.getField();
//            String message = error.getDefaultMessage();
//            map.put(fieldName, message);
//        }
//        return map;
//    }

    public void setCookie(HttpServletRequest request, HttpServletResponse response,
                          String cookieName,String cookieValue,Integer maxAge) {
        try {
            cookieValue = URLEncoder.encode(cookieValue, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
       setCookieValue(request,response,cookieName,cookieValue,maxAge);

    }

    public void setCookieValue(HttpServletRequest request, HttpServletResponse response,
                               String cookieName,String cookieValue,Integer maxAge) {
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setMaxAge(maxAge);
//        cookie.setDomain("imoocnews.com");
        cookie.setDomain(DOMAIN_NAME);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public void deleteCookie(HttpServletRequest request, HttpServletResponse response,
                             String cookieName) {
            setCookie(request,response,cookieName,"",COOKIE_DELETE);

    }

    /**
     * 根据key获取redis中的value
     * @param key
     * @return
     */
    public String getCountsFromRedis(String key) {
        String counts = redis.get(key);
        if (StringUtils.isBlank(counts)) {
            counts = "0";
        }
        return counts;
    }




}
